Advertisement
Guest User

backup-mail-and-secrets

a guest
Oct 20th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.25 KB | None | 0 0
  1. #!/bin/bash
  2. function exit_error(){
  3.     case "$1" in
  4.         0)
  5.             notify-send 'Backup Script Failed' 'Reason: Internet is down'
  6.             ;;
  7.         1)
  8.             notify-send 'Backup Script Failed' 'Reason: Error archiving folder(s)/file(s)'
  9.             ;;
  10.         2)
  11.             notify-send 'Backup Script Failed' 'Reason: Error encrypting file(s)'
  12.             ;;
  13.         3)
  14.             notify-send 'Backup Script Failed' 'Reason: Error copying file to myserver.com'
  15.             ;;
  16.         4)
  17.             notify-send 'Backup Script Failed' 'Reason: md5sum mismatch'
  18.             ;;
  19.     esac
  20.     exit
  21. }
  22.  
  23. function set_vars(){
  24.     case "$1" in
  25.         0)
  26.             remote_mail_sha512="$(ssh user@myserver.com 'sha512sum /home/admin/web/myserver.com/public_html/mail.tar.gz.gpg' | grep -oe '[0-9a-f]\{128\}')"
  27.             local_mail_sha512="$(sha512sum '/home/user/mail.tar.gz.gpg' | grep -oe '[0-9a-f]\{128\}')"
  28.             export remote_mail_sha512 local_mail_sha512
  29.             ;;
  30.         1)
  31.             remote_kdbx_sha512="$(ssh user@myserver.com 'sha512sum /home/admin/web/myserver.com/public_html/mpw.kdbx' | grep -oe '[0-9a-f]\{128\}')"
  32.             local_kdbx_sha512="$(sha512sum '/home/user/Documents/mpw.kdbx' | grep -oe '[0-9a-f]\{128\}')"
  33.             export remote_kdbx_sha512 local_kdbx_sha512
  34.             ;;
  35.     esac
  36. }
  37.  
  38. sleep 15
  39. set_display_preferences
  40. ping 1.1.1.1 -c1 -w1 || exit_error 0
  41. mail_modified='false'
  42.  
  43. for mailbox in /home/user/Mail/*; do
  44.     [[ "$mailbox" -nt '/home/user/mail-last-modified' ]] && mail_modified='true'
  45. done
  46.  
  47. phone_id="$(kdeconnect-cli -l | grep 'Sony.*paired and reachable' | grep -oe '[0-9a-f]\{16\}')"
  48. set_vars 0
  49. if "$mail_modified" || [[ "$local_mail_sha512" != "$remote_mail_sha512" ]]; then
  50.     touch '/home/user/mail-last-modified'
  51.    
  52.     tar czf '/home/user/mail.tar.gz' '/home/user/Mail' || exit_error 1
  53.     gpg --batch --yes --output '/home/user/mail.tar.gz.gpg' --encrypt --recipient user@mydomain.com '/home/user/mail.tar.gz' || exit_error 2
  54.     rm -f '/home/user/mail.tar.gz'
  55.     scp '/home/user/mail.tar.gz.gpg' 'user@mydomain.com:/home/admin/web/mydomain.com/public_html/' || exit_error 3
  56.    
  57.     set_vars 0
  58.     [[ "$local_mail_sha512" == "$remote_mail_sha512" ]] || exit_error 4
  59.    
  60.     [[ -z "$phone_id" ]] || cp '/home/user/mail.tar.gz.gpg' "/run/user/1000/${phone_id}/storage/emulated/0/Download/mail.tar.gz.gpg"
  61.    
  62.     uploaded_files=('mail.tar.gz.gpg')
  63. fi
  64.  
  65. set_vars 1
  66. if [[ "$local_kdbx_sha512" != "$remote_kdbx_sha512" ]]; then
  67.     scp '/home/user/Documents/mpw.kdbx' 'user@mydomain.com:/home/admin/web/mydomain.com/public_html/' || exit_error 3
  68.    
  69.     set_vars 1
  70.     [[ "$local_kdbx_sha512" == "$remote_kdbx_sha512" ]] || exit_error 4
  71.    
  72.     [[ -z "$phone_id" ]] || cp '/home/user/Documents/mpw.kdbx' "/run/user/1000/${phone_id}/storage/emulated/0/Download/mpw.kdbx"
  73.    
  74.     uploaded_files+=('mpw.kdbx')
  75. fi
  76.  
  77. if [[ ! -z "${uploaded_files[*]}" ]]; then
  78.     file_list="${uploaded_files[*]}"
  79.     notify-send 'Backup Script Successful' "Successfully uploaded:\\n${file_list// /$'\n'}"
  80.     [[ -z "$phone_id" ]] || kdeconnect-cli -d "$phone_id" --ping-msg "Received files: ${file_list// /, }"
  81. else
  82.     notify-send 'Backup Script Successful' 'No uploads were needed'
  83. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement